package com.kly.chatter.api.application.service.impl;

import com.kly.chatter.api.application.config.NacosValueConfig;
import com.kly.chatter.api.application.controller.chatter.res.DashboardRes;
import com.kly.chatter.api.application.service.ChatterDashboardInfoService;
import com.kly.chatter.db.dao.ChatterDashboardInfoDao;
import com.kly.chatter.db.entity.ChatterDashboardInfoEntity;
import com.kly.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class ChatterDashboardInfoServiceImpl implements ChatterDashboardInfoService {
    @Resource
    private NacosValueConfig nacosValueConfig;

    @Autowired
    private ChatterDashboardInfoDao chatterDashboardInfoDao;

    @Resource
    private UserService userService;

    @Override
    public DashboardRes getChatterDashboardInfo(String userCode, Integer date) {
        if (date == 1) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            date = Integer.parseInt(sdf.format(new Date()));
        }
        ChatterDashboardInfoEntity chatterDashboardInfo = chatterDashboardInfoDao.getDashboardInfo(userCode, date);
        DashboardRes dashboardRes = DashboardRes.toRes(chatterDashboardInfo);
        Integer chatterRating = userService.getChatterRating(userCode);
        dashboardRes.setChatterRating(chatterRating);
        if (nacosValueConfig.cleanCacheTipsSwitch()) {
            dashboardRes.setCleanCacheTips(nacosValueConfig.cleanCacheTips());
        }
        return dashboardRes;
    }
}
